﻿using System;
using System.IO;
using System.Web.UI;

namespace Rio.Applications.Web
{
    public partial class OpenPDF : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string folder = Server.UrlDecode(Request.QueryString["fld"]);

            try
            {
                if (File.Exists(folder))
                {
                    FileInfo file = new FileInfo(folder);

                    // Clear the content of the response   
                    Response.ClearContent();

                    //Set name 
                    Response.AddHeader(
                        "Content-Disposition",
                        String.Format("attachment; filename=Print_{0}_{1}.pdf", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString()));

                    // Add the file size into the response header    
                    Response.AddHeader("Content-Length", file.Length.ToString());

                    // Set the ContentType. function "ReturnExtension" return the proper content type and set it to the Response.ContentType property (not in this listing)    
                    Response.ContentType = "application/pdf";

                    // Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)    
                    Response.TransmitFile(file.FullName);
                }
                else
                {
                    Response.StatusCode = 404;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}